﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace transmission
{
    /// <summary>
    /// UDPCreate.xaml 的交互逻辑
    /// </summary>
    public partial class UDPCreate : Window
    {
      
        public UDPCreate()
        {
            InitializeComponent();
            this.ResizeMode = ResizeMode.NoResize;
            Left = MainWindow.Instance.Left + MainWindow.Instance.Width / 2 - Width / 2;
            Top = MainWindow.Instance.Top + MainWindow.Instance.Height / 2 - Height / 2;
            remoteIPText.Text = string.Format("{0}", Tools.GetLocalhostIP());
            remotePortText.Text = string.Format("{0}", 5555);
            localIPText.Text = string.Format("{0}", "0.0.0.0");
            localPortText.Text = string.Format("{0}", 0);
            groupIPText.Text = string.Format("{0}", "224.1.1.1");
            groupPortText.Text = string.Format("{0}", 65000);
            groupLocalIPText.Text = string.Format("{0}", "0.0.0.0");
        }

        private void OnIPInputText(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !Regex.IsMatch(e.Text, @"^[1-9.]*\d*|0$");
        }

        private void OnPortInputText(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !Regex.IsMatch(e.Text, @"^[1-9]\d*|0$");
        }

        private void OnSureClicked(object sender, RoutedEventArgs e)
        {
            if(udpGrid.Visibility == Visibility.Visible)
            {
                if (!Tools.ValidateIPAddress(remoteIPText.Text))
                {
                    MessageBox.Show("远程IP地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (!Tools.ValidatePort(remotePortText.Text))
                {
                    MessageBox.Show("远程Port地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (!Tools.ValidateIPAddress(localIPText.Text))
                {
                    MessageBox.Show("本地IP地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (!Tools.ValidatePort(localPortText.Text))
                {
                    MessageBox.Show("本地Port地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            else
            {
                if (!Tools.ValidateIPAddress(groupIPText.Text))
                {
                    MessageBox.Show("组播IP地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (!Tools.ValidatePort(groupPortText.Text))
                {
                    MessageBox.Show("组播Port地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (!Tools.ValidateIPAddress(groupLocalIPText.Text))
                {
                    MessageBox.Show("本地IP地址格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
            
            DialogResult = true;
        }

        public int GetGroupPort()
        {
            return int.Parse(groupPortText.Text.Trim());
        }
        public int GetRemotePort()
        {
            return int.Parse(remotePortText.Text.Trim());
        }
        public int GetLocalPort()
        {
            return int.Parse(localPortText.Text.Trim());
        }

        public string GetGroupIP()
        {
            return groupIPText.Text.Trim();
        }
        public string GetRemoteIP()
        {
            return remoteIPText.Text.Trim();
        }
        public string GetLocalIP()
        {
            return udpGrid.Visibility == Visibility.Visible ? localIPText.Text.Trim() : groupLocalIPText.Text.Trim();
        }

        public bool GetIsReliable()
        {
            return isReliable.IsChecked == true;
        }

        private void OnModeClicked(object sender, RoutedEventArgs e)
        {
            udpGrid.Visibility = udpGrid.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible;
            if (udpGrid.Visibility == Visibility.Visible)
                modeBtn.Content = "组播";
            else
                modeBtn.Content = "UDP";
            groupGrid.Visibility = udpGrid.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible;
            isReliable.IsChecked = false;
            isReliable.IsEnabled = false;
        }

        private void OnBroadcastClicked(object sender, RoutedEventArgs e)
        {
            string[] addrs = Tools.GetLocalhostIP().Split('.');
            if(addrs.Length == 4)
            {
                addrs[3] = "255";
                remoteIPText.Text = string.Format("{0}", string.Join(".", addrs));
            }
            else
                remoteIPText.Text = string.Format("{0}", Tools.GetLocalhostIP());
            isReliable.IsChecked = false;
            isReliable.IsEnabled = false;
        }
    }
}
